from project.models import Project
from pms.common.utils import flatten_view
import datetime


def update_project_by_view(pid, view):
    proj = Project.objects.filter(id=pid)
    argv = flatten_view(view, {
        'name': 'name',
        'leader': ['leader', 'id'],
        'current_version': 'current_version',
        'detail': 'detail'
    })
    proj.update(**argv)
    if 'start_time' in view:
        date = view['start_time']
        start_time = datetime.date(date.get('year'), date.get('month'), date.get('day'))
        proj.update(start_time=start_time)
    return proj[0]
